package com.ba360.struts.action;

import com.ba360.portal.database.DBConnectionImpl;
import com.ba360.portal.util.EmailSender;
import com.ba360.portal.util.Log;
import com.ba360.struts.form.ForgotPassword;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.naming.Context;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ForgotPasswordAction extends Action
{
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    ForgotPassword ForgotPassword = (ForgotPassword)form;
    String forward = "failure";
    Log.init(getClass());
    Context context = null;
    DataSource ds = null;
    Connection conn = null;
    Statement stmt = null;

    DBConnectionImpl dbConn = new DBConnectionImpl();
    conn = dbConn.getConnection();
    stmt = conn.createStatement();
    String action = request.getParameter("action");
    if ((action != null) && 
      (action.equals("Reset")))
    {
      String userId = dLookUp(stmt, "user_profile", "userid", 
        "email='" + ForgotPassword.getEmailAddress() + 
        "'");
      Log.info("For Forgot Passs :" + userId + " email:" + 
        ForgotPassword.getEmailAddress());
      if ((userId != null) && (userId.length() > 0)) {
        forward = "success";
        request.setAttribute("FORGOTPASS", "FORGOTPASS");
        Properties prop = new Properties();
        String email = dLookUp(stmt, "user_profile", "emil", "userid=" + Integer.parseInt(userId));
        String pwd = dLookUp(stmt, "user_profile", "password", "userid=" + Integer.parseInt(userId));

        prop.setProperty("email", ForgotPassword.getEmailAddress());
        prop.setProperty("Password", pwd);
        try {
          EmailSender.sendEmail(EmailSender.TYPE_RESETPASS, prop);
        }
        catch (Exception e) {
          Log.error(e.getMessage());
        }
      }
      else
      {
        request.setAttribute("NOUSER", "USERNOTFOUND");
      }

    }

    conn.close();
    return mapping.findForward(forward);
  }

  ResultSet openrs(Statement stat, String sql) throws SQLException
  {
    ResultSet rs = stat.executeQuery(sql);
    return rs;
  }

  private String dLookUp(Statement stat, String table, String fName, String where) {
    String res = null;
    Log.info("UserId:" + res + "  " + "SELECT " + fName + " FROM " + table + 
      " WHERE " + where);
    try {
      ResultSet rs1 = openrs(stat, "SELECT " + fName + " FROM " + 
        table + " WHERE " + where);
      rs1.next();
      res = rs1.getString(1);
      Log.info("UserId:" + res + "  " + "SELECT " + fName + " FROM " + 
        table + " WHERE " + where);
      rs1.close();
    }
    catch (Exception e) {
      Log.error(e.getMessage());
    }
    return res;
  }
}